import pandas as pd
import statsmodels.api as sm
import matplotlib.pyplot as plt
import matplotlib as mpl

mpl.rcParams['font.sans-serif'] = ['KaiTi']
mpl.rcParams['font.serif'] = ['KaiTi']

df = pd.read_excel('C:/Users/zlsjNKJS/Desktop/090-2/train.xlsx')

X = df[['Energy_standard']].values
y = df[['log消耗标准化']].values

X = sm.add_constant(X)
model = sm.OLS(y,X).fit()

print(model.summary())

plt.scatter(X[:,1],y,color='blue')
plt.plot(X[:,1], model.predict(X), color='red')

r_squared = model.rsquared
plt.text(0.95, 0.01, f'R-squared: {r_squared:.3f}',
         transform=plt.gca().transAxes,
         horizontalalignment='right')

coefficients = model.params
intercept = coefficients[0]
slope = coefficients[1]
equation = f'y = {intercept:.3f} + {slope:.3f}x'

plt.text(0.5, 0.5, equation, transform=plt.gca().transAxes, fontsize=12)

plt.xlabel('Energy_standard')
plt.ylabel('log消耗标准化')
plt.show()